#ifndef COMMON_H
#define COMMON_H


#if defined(ARDUINO) && ARDUINO >= 100
	#include "Arduino.h"
#else
	#include "WProgram.h"
#endif

#include "RF24.h"
#include <EEPROM.h>
#define LEDLIB_GRB_ORDER  1       // GRB leds
#include "LEDFx.h"

#undef SHOW_REFRESH_RATE
#undef PRINT_DEBUG
#define FADE_RATE 17

extern unsigned long dataStartTime;
extern unsigned long refreshCount;

//-- Program flow & State
enum Mode { 
// [Mode Byte] [Low Brightness] [High Brightness] [Saturation]
// expects that 8 leg data segments will be sent  
  Sound,
  Rainbow,
  Wave, 
  Solid,  
  Last = Solid,
  Loading,
  Disconnect,
};

#define DEFAULT_PACKET_SIZE 5

#define PIPE1 0xF0F0F0F0E1LL
#define PIPE2 0xF0F0F0F0C1LL
#define LED_COUNT         256     // total LED count
#define LEG_COUNT         16      
#define MIRRORED_LEGS     (LEG_COUNT/2)
#define BUCKET_SIZE       ((LED_COUNT/2)/MIRRORED_LEGS)

struct LegData {
  uint8_t legNumber;
  uint8_t value;
  uint8_t hue;
};

struct ModeMessage {
  Mode mode;
  uint8_t arg1;
  uint8_t arg2;
  uint8_t arg3;
  ModeMessage(uint8_t * buffer) : 
  mode((Mode)buffer[1]), arg1(buffer[2]), arg2(buffer[3]), arg3(buffer[4])
  {
  }
  ModeMessage() : 
  mode(Disconnect), arg1(0), arg2(0), arg3(0)
  {
  }
  ModeMessage(Mode m, uint8_t a1, uint8_t a2, uint8_t a3) :
    mode(m), arg1(a1), arg2(a2), arg3(a3) {
  }
  void copyFrom(uint8_t * buffer) {
    mode = (Mode)buffer[1];
    arg1 = buffer[2];
    arg2 = buffer[3];
    arg3 = buffer[4];
  }

  void print() {
      Serial.print("Mode: ");
      Serial.print(mode);
      Serial.print(" arg1: ");
      Serial.print(arg1);
      Serial.print(" arg2: ");
      Serial.print(arg2);
      Serial.print(" arg3: ");
      Serial.print(arg3);
      Serial.println();
      
  }
  void copyTo(uint8_t * buffer) {
    buffer[0] = 1;
    buffer[1] = mode;
    buffer[2] = arg1;
    buffer[3] = arg2;
    buffer[4] = arg3;
  }
};

extern uint8_t audioBreak[5];

float mapfloat(float x, float in_min, float in_max, float out_min, float out_max);
void initRadio(RF24 radio, bool reading);

uint16_t readInt16FromEEprom(int wordAddress);
void writeInt16ToEEPRom(int wordAddress, uint16_t value);
void drawAudioData(LegData *audioData, RGB *buffer, float hueShift, uint8_t saturation, uint8_t dimV, uint8_t brightV);
uint8_t bucketColor(int bucketNum, float colorShift);
#endif